/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved.
 * secPaver is licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

package sehandle

// GetAllRoles return all roles of policy
func (h *Handle) GetAllRoles() []string {
	var rs []string
	copy(rs, h.roles)
	return rs
}

func (h *Handle) RoleExist(role string) bool {
	for _, r := range h.roles {
		if r == role {
			return true
		}
	}

	return false
}

func (h *Handle) UserExist(user string) bool {
	for _, r := range h.users {
		if r == user {
			return true
		}
	}

	return false
}

// TypeExist checks if a SELinux type has been defined in system
func (h *Handle) TypeExist(tp string) bool {
	for _, info := range h.typeInfos {
		if info.Name == tp {
			return info.IsAttr == false
		}
	}

	return false
}

// AttributeExist checks if a SELinux attribute has been defined in system
func (h *Handle) AttributeExist(attr string) bool {
	for _, info := range h.typeInfos {
		if info.Name == attr {
			return info.IsAttr == true
		}
	}

	return false
}

func (h *Handle) RemoveMaskType(tp string) {
	for i, info := range h.typeInfos {
		if info.Name == tp && !info.IsAttr {
			tmp := h.typeInfos[i+1:]
			h.typeInfos = append(h.typeInfos[:i], tmp...)
		}
	}
}
